package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.Dish;
import com.langhua.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/dishs")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 分页查询
     * @param current  当前页
     * @param size     每页条数
     * @param dish   条件查询
     * @return
     */
    @GetMapping("{current}/{size}/{cd}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size, @PathVariable Integer cd, Dish dish){
        System.out.println("dish");
        System.out.println(dish);

        IPage page=new Page(current,size);
        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<Dish>();

        if (cd==1){
            //后台分页查询
            System.out.println("cd:");
            System.out.println(cd);
//            lqw.eq(Dish::getStatus,1);
            lqw.like(Strings.isNotEmpty(dish.getName()),Dish::getName,dish.getName());
        } else if (cd==2){
            //前台菜品推荐分页查询
            System.out.println("cd:");
            System.out.println(cd);
            lqw.eq(Dish::getIsRecommend,"true");
        }else if (cd==3){
            System.out.println("cd:");
            System.out.println(cd);
            lqw.eq(Dish::getFlavorType,dish.getFlavorType());
        }


        IPage page1 = dishService.page(page,lqw);
        System.out.println("菜品分页查询内容："+page1.toString());
        return R.success(page1,"dish分页查询");
    }

    /**
     * 分页查询
     * @param dish   条件查询
     * @return
     */
    @GetMapping("/getAll")
    public R< List<Dish>> getAll(Dish dish){
        System.out.println("dish");
        System.out.println(dish);

        LambdaQueryWrapper<Dish> lqw=new LambdaQueryWrapper<Dish>();
        lqw.eq(Strings.isNotEmpty(dish.getFlavorType()),Dish::getFlavorType,dish.getFlavorType());

        List<Dish> list = dishService.list(lqw);
        return R.success(list,"dish查询所有");
    }



    /**
     * 查询所有菜品类型(去重)
     * @return
     */
    @GetMapping
    public R<List<Dish>> getPage(){

        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.select("DISTINCT flavor_type");
        List<Dish> list = dishService.list(dishQueryWrapper);
        System.out.println("dish查询所有菜品类型(去重)："+list);
        return R.success(list,"dish查询所有菜品类型(去重)");
    }
    /**
     * 查询 根据类型查询菜品
     * @return
     */
    @GetMapping("/getMenuByFlavorType")
    public R<List<Dish>> getMenuByFlavorType(@RequestParam String flavortype){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getFlavorType,flavortype);
        List<Dish> list = dishService.list(lqw);

        System.out.println("dish根据类型查询菜品："+list);
        return R.success(list,"dish根据类型查询菜品");
    }

    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @PostMapping("/add")
    public R<String> save(HttpServletRequest request, @RequestBody Dish dish){
        log.info("新增员工，员工信息：{}",dish.toString());
        System.out.println("新增得id："+dish);
        dishService.save(dish);

        return R.success("新增菜品成功",null);
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Dish> getById(@PathVariable Long id){
        log.info("根据id查询菜品信息..."+id);
        Dish dish = dishService.getById(id);
        if(dish != null){
            return R.success(dish,null);
        }
        return R.error("没有查询到对应菜品信息");
    }

    /**
     * 根据id修改员工信息()
     * @param dish
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Dish dish){
        log.info(dish.toString());

        long id = Thread.currentThread().getId();
        log.info("修改id为：{}",id);
        //Long empId = (Long)request.getSession().getAttribute("admin");
        //admin.setUpdateTime(LocalDateTime.now());
        //admin.setUpdateUser(empId);

        dishService.updateById(dish);

        return R.success("菜品信息修改成功",null);
    }

    /**
     * 通过id删除Dish
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R<String> deleteById(@PathVariable Long id){
        log.info("删除Dish的id为：{}", id);

        dishService.removeById(id);
        return R.success(null,"删除成功！");
    }


    /**
     * 通过id修改菜品推荐状态
     * @return
     */
    @PutMapping("/updateIsRecommend")
    public R<Dish> updateIsRecommend(@RequestBody Dish dish){
        boolean b = dishService.updateById(dish);
        if (b){
            return R.success(dish,"通过id修改菜品推荐状态成功");
        }else {
            return R.error("通过id修改菜品推荐状态失败！");
        }
    }
}
